package com.programmercarl.greed;

/**
 * https://leetcode.cn/problems/gas-station/description/
 * 加油站  参考
 *
 * @author yulongtian
 * @create 2025-02-07 10:58
 */
public class Test08 {
    public static void main(String[] args) {

    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int curSum = 0;
        int totalSum = 0;
        int start = 0;
        for (int i = 0; i < gas.length; i++) {
            curSum += gas[i] - cost[i];
            totalSum += gas[i] - cost[i];
            if (curSum < 0) {
                curSum = 0;
                start = i + 1;
            }
        }
        if (totalSum < 0) {
            return -1;
        }
        return start;
    }
}
